<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>6.2 函数功能 | 商业数据分析师-R 语言数据处理</title>
  <meta name="description" content="商业数据分析师的 R 语言学习笔记,主要集中在数据清洗相关包的介绍，从数据导入(本地数据 数据库)到数据清洗，到固定报表输出的自动化流程。" />
  <meta name="generator" content="bookdown 0.22 and GitBook 2.6.7" />

  <meta property="og:title" content="6.2 函数功能 | 商业数据分析师-R 语言数据处理" />
  <meta property="og:type" content="book" />
  
  
  <meta property="og:description" content="商业数据分析师的 R 语言学习笔记,主要集中在数据清洗相关包的介绍，从数据导入(本地数据 数据库)到数据清洗，到固定报表输出的自动化流程。" />
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="6.2 函数功能 | 商业数据分析师-R 语言数据处理" />
  
  <meta name="twitter:description" content="商业数据分析师的 R 语言学习笔记,主要集中在数据清洗相关包的介绍，从数据导入(本地数据 数据库)到数据清洗，到固定报表输出的自动化流程。" />
  

<meta name="author" content="Yufei Zhong" />


<meta name="date" content="2021-06-21" />

  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="介绍.html"/>
<link rel="next" href="Data-Manipulation-datatable.html"/>
<script src="libs/header-attrs-2.8/header-attrs.js"></script>
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />









<link href="libs/anchor-sections-1.0.1/anchor-sections.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.0.1/anchor-sections.js"></script>


<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>


<link rel="stylesheet" href="style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./">R学习笔记</a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>欢迎</a>
<ul>
<li class="chapter" data-level="" data-path="abstract.html"><a href="abstract.html"><i class="fa fa-check"></i>内容概要</a></li>
<li class="chapter" data-level="" data-path="sec-licenses.html"><a href="sec-licenses.html"><i class="fa fa-check"></i>授权说明</a></li>
<li class="chapter" data-level="" data-path="session.html"><a href="session.html"><i class="fa fa-check"></i>运行信息</a></li>
<li class="chapter" data-level="" data-path="author.html"><a href="author.html"><i class="fa fa-check"></i>关于本人</a></li>
</ul></li>
<li class="chapter" data-level="1" data-path="data-read-write-description.html"><a href="data-read-write-description.html"><i class="fa fa-check"></i><b>1</b> 数据导入导出</a>
<ul>
<li class="chapter" data-level="1.1" data-path="readxl-description.html"><a href="readxl-description.html"><i class="fa fa-check"></i><b>1.1</b> readxl</a>
<ul>
<li class="chapter" data-level="1.1.1" data-path="readxl-description.html"><a href="readxl-description.html#readxl:install"><i class="fa fa-check"></i><b>1.1.1</b> 安装</a></li>
<li class="chapter" data-level="1.1.2" data-path="readxl-description.html"><a href="readxl-description.html#readxl:usage"><i class="fa fa-check"></i><b>1.1.2</b> 用法</a></li>
<li class="chapter" data-level="1.1.3" data-path="readxl-description.html"><a href="readxl-description.html#batch:read"><i class="fa fa-check"></i><b>1.1.3</b> 批量读取</a></li>
<li class="chapter" data-level="1.1.4" data-path="readxl-description.html"><a href="readxl-description.html#批量输出"><i class="fa fa-check"></i><b>1.1.4</b> 批量输出</a></li>
</ul></li>
<li class="chapter" data-level="1.2" data-path="writexl.html"><a href="writexl.html"><i class="fa fa-check"></i><b>1.2</b> writexl</a>
<ul>
<li class="chapter" data-level="1.2.1" data-path="writexl.html"><a href="writexl.html#writexl:usage"><i class="fa fa-check"></i><b>1.2.1</b> 用法</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="openxlsx-description.html"><a href="openxlsx-description.html"><i class="fa fa-check"></i><b>1.3</b> openxlsx</a>
<ul>
<li class="chapter" data-level="1.3.1" data-path="openxlsx-description.html"><a href="openxlsx-description.html#openxlsx:install"><i class="fa fa-check"></i><b>1.3.1</b> 安装</a></li>
<li class="chapter" data-level="1.3.2" data-path="openxlsx-description.html"><a href="openxlsx-description.html#openxlsx:functions"><i class="fa fa-check"></i><b>1.3.2</b> 基础功能</a></li>
<li class="chapter" data-level="1.3.3" data-path="openxlsx-description.html"><a href="openxlsx-description.html#定制格式输出"><i class="fa fa-check"></i><b>1.3.3</b> 定制格式输出</a></li>
<li class="chapter" data-level="1.3.4" data-path="openxlsx-description.html"><a href="openxlsx-description.html#函数参数"><i class="fa fa-check"></i><b>1.3.4</b> 函数参数</a></li>
<li class="chapter" data-level="1.3.5" data-path="openxlsx-description.html"><a href="openxlsx-description.html#总结"><i class="fa fa-check"></i><b>1.3.5</b> 总结</a></li>
</ul></li>
<li class="chapter" data-level="1.4" data-path="readr-package.html"><a href="readr-package.html"><i class="fa fa-check"></i><b>1.4</b> readr</a>
<ul>
<li class="chapter" data-level="1.4.1" data-path="readr-package.html"><a href="readr-package.html#readr:install"><i class="fa fa-check"></i><b>1.4.1</b> 安装</a></li>
<li class="chapter" data-level="1.4.2" data-path="readr-package.html"><a href="readr-package.html#readr:usage"><i class="fa fa-check"></i><b>1.4.2</b> 用法</a></li>
<li class="chapter" data-level="1.4.3" data-path="readr-package.html"><a href="readr-package.html#readr:write-function"><i class="fa fa-check"></i><b>1.4.3</b> 导出功能</a></li>
<li class="chapter" data-level="1.4.4" data-path="readr-package.html"><a href="readr-package.html#总结-1"><i class="fa fa-check"></i><b>1.4.4</b> 总结</a></li>
</ul></li>
<li class="chapter" data-level="1.5" data-path="data-vroom.html"><a href="data-vroom.html"><i class="fa fa-check"></i><b>1.5</b> vroom</a>
<ul>
<li class="chapter" data-level="1.5.1" data-path="data-vroom.html"><a href="data-vroom.html#安装"><i class="fa fa-check"></i><b>1.5.1</b> 安装</a></li>
<li class="chapter" data-level="1.5.2" data-path="data-vroom.html"><a href="data-vroom.html#用法"><i class="fa fa-check"></i><b>1.5.2</b> 用法</a></li>
</ul></li>
<li class="chapter" data-level="1.6" data-path="data-rstudio-addins.html"><a href="data-rstudio-addins.html"><i class="fa fa-check"></i><b>1.6</b> Rstudio导入</a>
<ul>
<li class="chapter" data-level="1.6.1" data-path="data-rstudio-addins.html"><a href="data-rstudio-addins.html#rstudio-工具栏导入"><i class="fa fa-check"></i><b>1.6.1</b> rstudio 工具栏导入</a></li>
<li class="chapter" data-level="1.6.2" data-path="data-rstudio-addins.html"><a href="data-rstudio-addins.html#插件导入"><i class="fa fa-check"></i><b>1.6.2</b> 插件导入</a></li>
</ul></li>
<li class="chapter" data-level="1.7" data-path="data-file-path.html"><a href="data-file-path.html"><i class="fa fa-check"></i><b>1.7</b> 文件路径</a>
<ul>
<li class="chapter" data-level="1.7.1" data-path="data-file-path.html"><a href="data-file-path.html#指定路径"><i class="fa fa-check"></i><b>1.7.1</b> 指定路径</a></li>
<li class="chapter" data-level="1.7.2" data-path="data-file-path.html"><a href="data-file-path.html#默认路径"><i class="fa fa-check"></i><b>1.7.2</b> 默认路径</a></li>
</ul></li>
<li class="chapter" data-level="1.8" data-path="data-expand.html"><a href="data-expand.html"><i class="fa fa-check"></i><b>1.8</b> 拓展</a>
<ul>
<li class="chapter" data-level="1.8.1" data-path="data-expand.html"><a href="data-expand.html#tidyxl:package"><i class="fa fa-check"></i><b>1.8.1</b> tidyxl</a></li>
<li class="chapter" data-level="1.8.2" data-path="data-expand.html"><a href="data-expand.html#参考资料"><i class="fa fa-check"></i><b>1.8.2</b> 参考资料</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="2" data-path="Data-Manipulation-dplyr.html"><a href="Data-Manipulation-dplyr.html"><i class="fa fa-check"></i><b>2</b> 数据处理之-dplyr</a>
<ul>
<li class="chapter" data-level="2.1" data-path="dplyr-description.html"><a href="dplyr-description.html"><i class="fa fa-check"></i><b>2.1</b> 前言</a>
<ul>
<li class="chapter" data-level="2.1.1" data-path="dplyr-description.html"><a href="dplyr-description.html#dplyr:install-package"><i class="fa fa-check"></i><b>2.1.1</b> 安装</a></li>
<li class="chapter" data-level="2.1.2" data-path="dplyr-description.html"><a href="dplyr-description.html#dplyr:difference-of-sql"><i class="fa fa-check"></i><b>2.1.2</b> Excel and Sql 类比</a></li>
<li class="chapter" data-level="2.1.3" data-path="dplyr-description.html"><a href="dplyr-description.html#常见问题"><i class="fa fa-check"></i><b>2.1.3</b> 常见问题</a></li>
</ul></li>
<li class="chapter" data-level="2.2" data-path="dplyr-usage.html"><a href="dplyr-usage.html"><i class="fa fa-check"></i><b>2.2</b> 基础用法</a>
<ul>
<li class="chapter" data-level="2.2.1" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-filter"><i class="fa fa-check"></i><b>2.2.1</b> filter</a></li>
<li class="chapter" data-level="2.2.2" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-select"><i class="fa fa-check"></i><b>2.2.2</b> select</a></li>
<li class="chapter" data-level="2.2.3" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-rename"><i class="fa fa-check"></i><b>2.2.3</b> rename</a></li>
<li class="chapter" data-level="2.2.4" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-relocate"><i class="fa fa-check"></i><b>2.2.4</b> relocate</a></li>
<li class="chapter" data-level="2.2.5" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-mutate"><i class="fa fa-check"></i><b>2.2.5</b> mutate</a></li>
<li class="chapter" data-level="2.2.6" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-arrange"><i class="fa fa-check"></i><b>2.2.6</b> arrange</a></li>
<li class="chapter" data-level="2.2.7" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-summarise"><i class="fa fa-check"></i><b>2.2.7</b> summarise</a></li>
<li class="chapter" data-level="2.2.8" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-groupby"><i class="fa fa-check"></i><b>2.2.8</b> group_by</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="dplyr-functions.html"><a href="dplyr-functions.html"><i class="fa fa-check"></i><b>2.3</b> 常用函数</a>
<ul>
<li class="chapter" data-level="2.3.1" data-path="dplyr-functions.html"><a href="dplyr-functions.html#条件判断"><i class="fa fa-check"></i><b>2.3.1</b> 条件判断</a></li>
<li class="chapter" data-level="2.3.2" data-path="dplyr-functions.html"><a href="dplyr-functions.html#case_when"><i class="fa fa-check"></i><b>2.3.2</b> case_when</a></li>
<li class="chapter" data-level="2.3.3" data-path="dplyr-functions.html"><a href="dplyr-functions.html#计数函数"><i class="fa fa-check"></i><b>2.3.3</b> 计数函数</a></li>
<li class="chapter" data-level="2.3.4" data-path="dplyr-functions.html"><a href="dplyr-functions.html#排序函数"><i class="fa fa-check"></i><b>2.3.4</b> 排序函数</a></li>
<li class="chapter" data-level="2.3.5" data-path="dplyr-functions.html"><a href="dplyr-functions.html#提取向量"><i class="fa fa-check"></i><b>2.3.5</b> 提取向量</a></li>
<li class="chapter" data-level="2.3.6" data-path="dplyr-functions.html"><a href="dplyr-functions.html#dplyr:slice"><i class="fa fa-check"></i><b>2.3.6</b> slice 系列</a></li>
<li class="chapter" data-level="2.3.7" data-path="dplyr-functions.html"><a href="dplyr-functions.html#group-系列"><i class="fa fa-check"></i><b>2.3.7</b> group 系列</a></li>
<li class="chapter" data-level="2.3.8" data-path="dplyr-functions.html"><a href="dplyr-functions.html#其它函数"><i class="fa fa-check"></i><b>2.3.8</b> 其它函数</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="dplyr-merge-two-table.html"><a href="dplyr-merge-two-table.html"><i class="fa fa-check"></i><b>2.4</b> 表关联</a>
<ul>
<li class="chapter" data-level="2.4.1" data-path="dplyr-merge-two-table.html"><a href="dplyr-merge-two-table.html#dplyr:merge-table-usage"><i class="fa fa-check"></i><b>2.4.1</b> 两表关联</a></li>
<li class="chapter" data-level="2.4.2" data-path="dplyr-merge-two-table.html"><a href="dplyr-merge-two-table.html#dplyr:merge-multiple-table"><i class="fa fa-check"></i><b>2.4.2</b> 多表操作</a></li>
</ul></li>
<li class="chapter" data-level="2.5" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html"><i class="fa fa-check"></i><b>2.5</b> 分组操作</a>
<ul>
<li class="chapter" data-level="2.5.1" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#添加分组"><i class="fa fa-check"></i><b>2.5.1</b> 添加分组</a></li>
<li class="chapter" data-level="2.5.2" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#查看分组"><i class="fa fa-check"></i><b>2.5.2</b> 查看分组</a></li>
<li class="chapter" data-level="2.5.3" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#更改和添加分组变量"><i class="fa fa-check"></i><b>2.5.3</b> 更改和添加分组变量</a></li>
<li class="chapter" data-level="2.5.4" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#删除分组变量"><i class="fa fa-check"></i><b>2.5.4</b> 删除分组变量</a></li>
<li class="chapter" data-level="2.5.5" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#动词影响"><i class="fa fa-check"></i><b>2.5.5</b> 动词影响</a></li>
</ul></li>
<li class="chapter" data-level="2.6" data-path="dplyr-column-manipulation.html"><a href="dplyr-column-manipulation.html"><i class="fa fa-check"></i><b>2.6</b> 列操作</a>
<ul>
<li class="chapter" data-level="2.6.1" data-path="dplyr-column-manipulation.html"><a href="dplyr-column-manipulation.html#dplyr-column-manipulation-basic-usage"><i class="fa fa-check"></i><b>2.6.1</b> 基础用法</a></li>
<li class="chapter" data-level="2.6.2" data-path="dplyr-column-manipulation.html"><a href="dplyr-column-manipulation.html#多种函数功能"><i class="fa fa-check"></i><b>2.6.2</b> 多种函数功能</a></li>
<li class="chapter" data-level="2.6.3" data-path="dplyr-column-manipulation.html"><a href="dplyr-column-manipulation.html#当前列"><i class="fa fa-check"></i><b>2.6.3</b> 当前列</a></li>
</ul></li>
<li class="chapter" data-level="2.7" data-path="dplyr-row-manipulation.html"><a href="dplyr-row-manipulation.html"><i class="fa fa-check"></i><b>2.7</b> 行操作</a>
<ul>
<li class="chapter" data-level="2.7.1" data-path="dplyr-row-manipulation.html"><a href="dplyr-row-manipulation.html#比较差异"><i class="fa fa-check"></i><b>2.7.1</b> 比较差异</a></li>
<li class="chapter" data-level="2.7.2" data-path="dplyr-row-manipulation.html"><a href="dplyr-row-manipulation.html#常用案例"><i class="fa fa-check"></i><b>2.7.2</b> 常用案例</a></li>
</ul></li>
<li class="chapter" data-level="2.8" data-path="dplyr-programming.html"><a href="dplyr-programming.html"><i class="fa fa-check"></i><b>2.8</b> <code>dplyr</code>编程</a>
<ul>
<li class="chapter" data-level="2.8.1" data-path="dplyr-programming.html"><a href="dplyr-programming.html#案例"><i class="fa fa-check"></i><b>2.8.1</b> 案例</a></li>
</ul></li>
<li class="chapter" data-level="2.9" data-path="dplyr-reference-material.html"><a href="dplyr-reference-material.html"><i class="fa fa-check"></i><b>2.9</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="Data-tidyr-package.html"><a href="Data-tidyr-package.html"><i class="fa fa-check"></i><b>3</b> 整洁数据之 tidyr</a>
<ul>
<li class="chapter" data-level="3.1" data-path="tidyr-useage.html"><a href="tidyr-useage.html"><i class="fa fa-check"></i><b>3.1</b> tidyr 介绍</a>
<ul>
<li class="chapter" data-level="3.1.1" data-path="tidyr-useage.html"><a href="tidyr-useage.html#tidyr:install"><i class="fa fa-check"></i><b>3.1.1</b> 安装</a></li>
<li class="chapter" data-level="3.1.2" data-path="tidyr-useage.html"><a href="tidyr-useage.html#tidyr:functions"><i class="fa fa-check"></i><b>3.1.2</b> 主要功能</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="pivoting.html"><a href="pivoting.html"><i class="fa fa-check"></i><b>3.2</b> 长宽转换</a>
<ul>
<li class="chapter" data-level="3.2.1" data-path="pivoting.html"><a href="pivoting.html#宽转长"><i class="fa fa-check"></i><b>3.2.1</b> 宽转长</a></li>
<li class="chapter" data-level="3.2.2" data-path="pivoting.html"><a href="pivoting.html#用法-1"><i class="fa fa-check"></i><b>3.2.2</b> 用法</a></li>
<li class="chapter" data-level="3.2.3" data-path="pivoting.html"><a href="pivoting.html#长转宽"><i class="fa fa-check"></i><b>3.2.3</b> 长转宽</a></li>
<li class="chapter" data-level="3.2.4" data-path="pivoting.html"><a href="pivoting.html#用法-2"><i class="fa fa-check"></i><b>3.2.4</b> 用法</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="tidyr-rectangle.html"><a href="tidyr-rectangle.html"><i class="fa fa-check"></i><b>3.3</b> 矩形数据</a></li>
<li class="chapter" data-level="3.4" data-path="tidyr-nest-data.html"><a href="tidyr-nest-data.html"><i class="fa fa-check"></i><b>3.4</b> 嵌套数据</a>
<ul>
<li class="chapter" data-level="3.4.1" data-path="tidyr-nest-data.html"><a href="tidyr-nest-data.html#基础"><i class="fa fa-check"></i><b>3.4.1</b> 基础</a></li>
<li class="chapter" data-level="3.4.2" data-path="tidyr-nest-data.html"><a href="tidyr-nest-data.html#嵌套数据和模型"><i class="fa fa-check"></i><b>3.4.2</b> 嵌套数据和模型</a></li>
</ul></li>
<li class="chapter" data-level="3.5" data-path="character-vector.html"><a href="character-vector.html"><i class="fa fa-check"></i><b>3.5</b> 字符处理</a>
<ul>
<li class="chapter" data-level="3.5.1" data-path="character-vector.html"><a href="character-vector.html#新列提取"><i class="fa fa-check"></i><b>3.5.1</b> 新列提取</a></li>
<li class="chapter" data-level="3.5.2" data-path="character-vector.html"><a href="character-vector.html#拆分"><i class="fa fa-check"></i><b>3.5.2</b> 拆分</a></li>
<li class="chapter" data-level="3.5.3" data-path="character-vector.html"><a href="character-vector.html#列拆分行"><i class="fa fa-check"></i><b>3.5.3</b> 列拆分行</a></li>
<li class="chapter" data-level="3.5.4" data-path="character-vector.html"><a href="character-vector.html#合并多列"><i class="fa fa-check"></i><b>3.5.4</b> 合并多列</a></li>
</ul></li>
<li class="chapter" data-level="3.6" data-path="tidyr-expand.html"><a href="tidyr-expand.html"><i class="fa fa-check"></i><b>3.6</b> 扩展数据框</a></li>
<li class="chapter" data-level="3.7" data-path="tidyr-fill-NA.html"><a href="tidyr-fill-NA.html"><i class="fa fa-check"></i><b>3.7</b> 缺失值</a>
<ul>
<li class="chapter" data-level="3.7.1" data-path="tidyr-fill-NA.html"><a href="tidyr-fill-NA.html#replace_na"><i class="fa fa-check"></i><b>3.7.1</b> replace_na</a></li>
<li class="chapter" data-level="3.7.2" data-path="tidyr-fill-NA.html"><a href="tidyr-fill-NA.html#fill"><i class="fa fa-check"></i><b>3.7.2</b> fill</a></li>
<li class="chapter" data-level="3.7.3" data-path="tidyr-fill-NA.html"><a href="tidyr-fill-NA.html#drop_na"><i class="fa fa-check"></i><b>3.7.3</b> drop_na</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="character-handling.html"><a href="character-handling.html"><i class="fa fa-check"></i><b>4</b> 字符处理</a>
<ul>
<li class="chapter" data-level="4.1" data-path="character-base-R.html"><a href="character-base-R.html"><i class="fa fa-check"></i><b>4.1</b> base R</a>
<ul>
<li class="chapter" data-level="4.1.1" data-path="character-base-R.html"><a href="character-base-R.html#单双引号"><i class="fa fa-check"></i><b>4.1.1</b> 单双引号</a></li>
<li class="chapter" data-level="4.1.2" data-path="character-base-R.html"><a href="character-base-R.html#转义"><i class="fa fa-check"></i><b>4.1.2</b> 转义</a></li>
<li class="chapter" data-level="4.1.3" data-path="character-base-R.html"><a href="character-base-R.html#常用函数"><i class="fa fa-check"></i><b>4.1.3</b> 常用函数</a></li>
<li class="chapter" data-level="4.1.4" data-path="character-base-R.html"><a href="character-base-R.html#newfeatures"><i class="fa fa-check"></i><b>4.1.4</b> 新特性</a></li>
</ul></li>
<li class="chapter" data-level="4.2" data-path="character-stringr-packages.html"><a href="character-stringr-packages.html"><i class="fa fa-check"></i><b>4.2</b> stringr</a>
<ul>
<li class="chapter" data-level="4.2.1" data-path="character-stringr-packages.html"><a href="character-stringr-packages.html#stringr-install"><i class="fa fa-check"></i><b>4.2.1</b> 安装</a></li>
<li class="chapter" data-level="4.2.2" data-path="character-stringr-packages.html"><a href="character-stringr-packages.html#stringr-usage"><i class="fa fa-check"></i><b>4.2.2</b> 基本使用</a></li>
<li class="chapter" data-level="4.2.3" data-path="character-stringr-packages.html"><a href="character-stringr-packages.html#stringr-functions"><i class="fa fa-check"></i><b>4.2.3</b> 常用函数</a></li>
</ul></li>
<li class="chapter" data-level="4.3" data-path="character-application.html"><a href="character-application.html"><i class="fa fa-check"></i><b>4.3</b> 综合运用</a>
<ul>
<li class="chapter" data-level="4.3.1" data-path="character-application.html"><a href="character-application.html#实现excel函数"><i class="fa fa-check"></i><b>4.3.1</b> 实现excel函数</a></li>
<li class="chapter" data-level="4.3.2" data-path="character-application.html"><a href="character-application.html#使用案例"><i class="fa fa-check"></i><b>4.3.2</b> 使用案例</a></li>
</ul></li>
<li class="chapter" data-level="4.4" data-path="character-the-difference-stringr-and-base.html"><a href="character-the-difference-stringr-and-base.html"><i class="fa fa-check"></i><b>4.4</b> base 和 stringr</a></li>
<li class="chapter" data-level="4.5" data-path="character-reference-material.html"><a href="character-reference-material.html"><i class="fa fa-check"></i><b>4.5</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="datetime.html"><a href="datetime.html"><i class="fa fa-check"></i><b>5</b> 时间处理</a>
<ul>
<li class="chapter" data-level="5.1" data-path="datetime-base-R.html"><a href="datetime-base-R.html"><i class="fa fa-check"></i><b>5.1</b> base R</a>
<ul>
<li class="chapter" data-level="5.1.1" data-path="datetime-base-R.html"><a href="datetime-base-R.html#the-date-class"><i class="fa fa-check"></i><b>5.1.1</b> Date</a></li>
<li class="chapter" data-level="5.1.2" data-path="datetime-base-R.html"><a href="datetime-base-R.html#the-POSIXct-class"><i class="fa fa-check"></i><b>5.1.2</b> POSIXct</a></li>
<li class="chapter" data-level="5.1.3" data-path="datetime-base-R.html"><a href="datetime-base-R.html#the-POSIXlt-class"><i class="fa fa-check"></i><b>5.1.3</b> POSIXlt</a></li>
</ul></li>
<li class="chapter" data-level="5.2" data-path="lubridate.html"><a href="lubridate.html"><i class="fa fa-check"></i><b>5.2</b> lubridate</a>
<ul>
<li class="chapter" data-level="5.2.1" data-path="lubridate.html"><a href="lubridate.html#lubridate-install"><i class="fa fa-check"></i><b>5.2.1</b> 安装包</a></li>
<li class="chapter" data-level="5.2.2" data-path="lubridate.html"><a href="lubridate.html#get-current-datetime"><i class="fa fa-check"></i><b>5.2.2</b> 当前时间日期</a></li>
<li class="chapter" data-level="5.2.3" data-path="lubridate.html"><a href="lubridate.html#make-datetime"><i class="fa fa-check"></i><b>5.2.3</b> 构造日期时间</a></li>
<li class="chapter" data-level="5.2.4" data-path="lubridate.html"><a href="lubridate.html#parse-datetime"><i class="fa fa-check"></i><b>5.2.4</b> 解析日期时间</a></li>
<li class="chapter" data-level="5.2.5" data-path="lubridate.html"><a href="lubridate.html#extracting-datetime-information"><i class="fa fa-check"></i><b>5.2.5</b> 提取日期时间成分</a></li>
<li class="chapter" data-level="5.2.6" data-path="lubridate.html"><a href="lubridate.html#time-zones"><i class="fa fa-check"></i><b>5.2.6</b> 处理时区</a></li>
<li class="chapter" data-level="5.2.7" data-path="lubridate.html"><a href="lubridate.html#interveal"><i class="fa fa-check"></i><b>5.2.7</b> 时间间隔</a></li>
<li class="chapter" data-level="5.2.8" data-path="lubridate.html"><a href="lubridate.html#calculate-datetime"><i class="fa fa-check"></i><b>5.2.8</b> 时间日期计算</a></li>
</ul></li>
<li class="chapter" data-level="5.3" data-path="datetime-application.html"><a href="datetime-application.html"><i class="fa fa-check"></i><b>5.3</b> 综合运用</a>
<ul>
<li class="chapter" data-level="5.3.1" data-path="datetime-application.html"><a href="datetime-application.html#日报同环比计算"><i class="fa fa-check"></i><b>5.3.1</b> 日报同环比计算</a></li>
<li class="chapter" data-level="5.3.2" data-path="datetime-application.html"><a href="datetime-application.html#清洗不同类型日期格式"><i class="fa fa-check"></i><b>5.3.2</b> 清洗不同类型日期格式</a></li>
<li class="chapter" data-level="5.3.3" data-path="datetime-application.html"><a href="datetime-application.html#扫码后中奖时间匹配"><i class="fa fa-check"></i><b>5.3.3</b> 扫码后中奖时间匹配</a></li>
</ul></li>
<li class="chapter" data-level="5.4" data-path="datetime-additional-information.html"><a href="datetime-additional-information.html"><i class="fa fa-check"></i><b>5.4</b> 补充资料</a>
<ul>
<li class="chapter" data-level="5.4.1" data-path="datetime-additional-information.html"><a href="datetime-additional-information.html#excel-and-r"><i class="fa fa-check"></i><b>5.4.1</b> Excel and R</a></li>
<li class="chapter" data-level="5.4.2" data-path="datetime-additional-information.html"><a href="datetime-additional-information.html#参考资料-1"><i class="fa fa-check"></i><b>5.4.2</b> 参考资料</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="6" data-path="forcats.html"><a href="forcats.html"><i class="fa fa-check"></i><b>6</b> 因子介绍</a>
<ul>
<li class="chapter" data-level="6.1" data-path="介绍.html"><a href="介绍.html"><i class="fa fa-check"></i><b>6.1</b> 介绍</a>
<ul>
<li class="chapter" data-level="6.1.1" data-path="介绍.html"><a href="介绍.html#安装-1"><i class="fa fa-check"></i><b>6.1.1</b> 安装</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="函数功能.html"><a href="函数功能.html"><i class="fa fa-check"></i><b>6.2</b> 函数功能</a>
<ul>
<li class="chapter" data-level="6.2.1" data-path="函数功能.html"><a href="函数功能.html#创建因子"><i class="fa fa-check"></i><b>6.2.1</b> 创建因子</a></li>
<li class="chapter" data-level="6.2.2" data-path="函数功能.html"><a href="函数功能.html#修改因子顺序"><i class="fa fa-check"></i><b>6.2.2</b> 修改因子顺序</a></li>
<li class="chapter" data-level="6.2.3" data-path="函数功能.html"><a href="函数功能.html#修改因子级别"><i class="fa fa-check"></i><b>6.2.3</b> 修改因子级别</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="7" data-path="Data-Manipulation-datatable.html"><a href="Data-Manipulation-datatable.html"><i class="fa fa-check"></i><b>7</b> data.table</a>
<ul>
<li class="chapter" data-level="7.1" data-path="datatable-description.html"><a href="datatable-description.html"><i class="fa fa-check"></i><b>7.1</b> 基础介绍</a>
<ul>
<li class="chapter" data-level="7.1.1" data-path="datatable-description.html"><a href="datatable-description.html#读取数据"><i class="fa fa-check"></i><b>7.1.1</b> 读取数据</a></li>
<li class="chapter" data-level="7.1.2" data-path="datatable-description.html"><a href="datatable-description.html#基本格式"><i class="fa fa-check"></i><b>7.1.2</b> 基本格式</a></li>
<li class="chapter" data-level="7.1.3" data-path="datatable-description.html"><a href="datatable-description.html#i-j-by-使用"><i class="fa fa-check"></i><b>7.1.3</b> i j by 使用</a></li>
<li class="chapter" data-level="7.1.4" data-path="datatable-description.html"><a href="datatable-description.html#行列筛选总结"><i class="fa fa-check"></i><b>7.1.4</b> 行列筛选总结</a></li>
</ul></li>
<li class="chapter" data-level="7.2" data-path="datatable-usage.html"><a href="datatable-usage.html"><i class="fa fa-check"></i><b>7.2</b> 常规操作</a>
<ul>
<li class="chapter" data-level="7.2.1" data-path="datatable-usage.html"><a href="datatable-usage.html#行筛选"><i class="fa fa-check"></i><b>7.2.1</b> 行筛选</a></li>
<li class="chapter" data-level="7.2.2" data-path="datatable-usage.html"><a href="datatable-usage.html#新增更新列"><i class="fa fa-check"></i><b>7.2.2</b> 新增更新列</a></li>
<li class="chapter" data-level="7.2.3" data-path="datatable-usage.html"><a href="datatable-usage.html#排序"><i class="fa fa-check"></i><b>7.2.3</b> 排序</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="datatable-function.html"><a href="datatable-function.html"><i class="fa fa-check"></i><b>7.3</b> 常用函数</a>
<ul>
<li class="chapter" data-level="7.3.1" data-path="datatable-function.html"><a href="datatable-function.html#特殊符号"><i class="fa fa-check"></i><b>7.3.1</b> 特殊符号</a></li>
<li class="chapter" data-level="7.3.2" data-path="datatable-function.html"><a href="datatable-function.html#排序函数-1"><i class="fa fa-check"></i><b>7.3.2</b> 排序函数</a></li>
<li class="chapter" data-level="7.3.3" data-path="datatable-function.html"><a href="datatable-function.html#非重复计数"><i class="fa fa-check"></i><b>7.3.3</b> 非重复计数</a></li>
<li class="chapter" data-level="7.3.4" data-path="datatable-function.html"><a href="datatable-function.html#判断函数"><i class="fa fa-check"></i><b>7.3.4</b> 判断函数</a></li>
<li class="chapter" data-level="7.3.5" data-path="datatable-function.html"><a href="datatable-function.html#交集-差集-合并"><i class="fa fa-check"></i><b>7.3.5</b> 交集 差集 合并</a></li>
<li class="chapter" data-level="7.3.6" data-path="datatable-function.html"><a href="datatable-function.html#长宽转换"><i class="fa fa-check"></i><b>7.3.6</b> 长宽转换</a></li>
<li class="chapter" data-level="7.3.7" data-path="datatable-function.html"><a href="datatable-function.html#表连接"><i class="fa fa-check"></i><b>7.3.7</b> 表连接</a></li>
</ul></li>
<li class="chapter" data-level="7.4" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html"><i class="fa fa-check"></i><b>7.4</b> 高级函数</a>
<ul>
<li class="chapter" data-level="7.4.1" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html#groupingsets"><i class="fa fa-check"></i><b>7.4.1</b> groupingsets</a></li>
<li class="chapter" data-level="7.4.2" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html#rleid"><i class="fa fa-check"></i><b>7.4.2</b> rleid</a></li>
<li class="chapter" data-level="7.4.3" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html#shift"><i class="fa fa-check"></i><b>7.4.3</b> shift</a></li>
<li class="chapter" data-level="7.4.4" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html#j"><i class="fa fa-check"></i><b>7.4.4</b> J</a></li>
</ul></li>
<li class="chapter" data-level="7.5" data-path="datatable-tips.html"><a href="datatable-tips.html"><i class="fa fa-check"></i><b>7.5</b> 小技巧</a>
<ul>
<li class="chapter" data-level="7.5.1" data-path="datatable-tips.html"><a href="datatable-tips.html#用抑制中间过程输出"><i class="fa fa-check"></i><b>7.5.1</b> 用{}抑制中间过程输出</a></li>
<li class="chapter" data-level="7.5.2" data-path="datatable-tips.html"><a href="datatable-tips.html#使用打印data.table"><i class="fa fa-check"></i><b>7.5.2</b> 使用[]打印data.table</a></li>
</ul></li>
<li class="chapter" data-level="7.6" data-path="datatable-application.html"><a href="datatable-application.html"><i class="fa fa-check"></i><b>7.6</b> 运用</a>
<ul>
<li class="chapter" data-level="7.6.1" data-path="datatable-application.html"><a href="datatable-application.html#自定义函数计算"><i class="fa fa-check"></i><b>7.6.1</b> 自定义函数计算</a></li>
<li class="chapter" data-level="7.6.2" data-path="datatable-application.html"><a href="datatable-application.html#带汇总的聚合运算"><i class="fa fa-check"></i><b>7.6.2</b> 带汇总的聚合运算</a></li>
<li class="chapter" data-level="7.6.3" data-path="datatable-application.html"><a href="datatable-application.html#行列转变"><i class="fa fa-check"></i><b>7.6.3</b> 行列转变</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="8" data-path="database.html"><a href="database.html"><i class="fa fa-check"></i><b>8</b> database</a>
<ul>
<li class="chapter" data-level="8.1" data-path="database-install.html"><a href="database-install.html"><i class="fa fa-check"></i><b>8.1</b> 安装数据库</a></li>
<li class="chapter" data-level="8.2" data-path="database-odbc-package.html"><a href="database-odbc-package.html"><i class="fa fa-check"></i><b>8.2</b> odbc包</a>
<ul>
<li class="chapter" data-level="8.2.1" data-path="database-odbc-package.html"><a href="database-odbc-package.html#安装包"><i class="fa fa-check"></i><b>8.2.1</b> 安装包</a></li>
<li class="chapter" data-level="8.2.2" data-path="database-odbc-package.html"><a href="database-odbc-package.html#驱动安装"><i class="fa fa-check"></i><b>8.2.2</b> 驱动安装</a></li>
<li class="chapter" data-level="8.2.3" data-path="database-odbc-package.html"><a href="database-odbc-package.html#连接数据库"><i class="fa fa-check"></i><b>8.2.3</b> 连接数据库</a></li>
<li class="chapter" data-level="8.2.4" data-path="database-odbc-package.html"><a href="database-odbc-package.html#读写数据库"><i class="fa fa-check"></i><b>8.2.4</b> 读写数据库</a></li>
</ul></li>
<li class="chapter" data-level="8.3" data-path="database-DBI-package.html"><a href="database-DBI-package.html"><i class="fa fa-check"></i><b>8.3</b> DBI包</a>
<ul>
<li class="chapter" data-level="8.3.1" data-path="database-DBI-package.html"><a href="database-DBI-package.html#安装-2"><i class="fa fa-check"></i><b>8.3.1</b> 安装</a></li>
<li class="chapter" data-level="8.3.2" data-path="database-DBI-package.html"><a href="database-DBI-package.html#连接数据库-1"><i class="fa fa-check"></i><b>8.3.2</b> 连接数据库</a></li>
<li class="chapter" data-level="8.3.3" data-path="database-DBI-package.html"><a href="database-DBI-package.html#读写数据库-1"><i class="fa fa-check"></i><b>8.3.3</b> 读写数据库</a></li>
<li class="chapter" data-level="8.3.4" data-path="database-DBI-package.html"><a href="database-DBI-package.html#其它函数介绍"><i class="fa fa-check"></i><b>8.3.4</b> 其它函数介绍</a></li>
</ul></li>
<li class="chapter" data-level="8.4" data-path="database-else-package.html"><a href="database-else-package.html"><i class="fa fa-check"></i><b>8.4</b> 其他R包</a>
<ul>
<li class="chapter" data-level="8.4.1" data-path="database-else-package.html"><a href="database-else-package.html#rodbc包"><i class="fa fa-check"></i><b>8.4.1</b> RODBC包</a></li>
<li class="chapter" data-level="8.4.2" data-path="database-else-package.html"><a href="database-else-package.html#roracle包"><i class="fa fa-check"></i><b>8.4.2</b> ROracle包</a></li>
<li class="chapter" data-level="8.4.3" data-path="database-else-package.html"><a href="database-else-package.html#rmysql包"><i class="fa fa-check"></i><b>8.4.3</b> RMySQL包</a></li>
<li class="chapter" data-level="8.4.4" data-path="database-else-package.html"><a href="database-else-package.html#rsqlite"><i class="fa fa-check"></i><b>8.4.4</b> RSQLite</a></li>
</ul></li>
<li class="chapter" data-level="8.5" data-path="database-big-data.html"><a href="database-big-data.html"><i class="fa fa-check"></i><b>8.5</b> 大数据相关R包</a>
<ul>
<li class="chapter" data-level="8.5.1" data-path="database-big-data.html"><a href="database-big-data.html#sparklyr"><i class="fa fa-check"></i><b>8.5.1</b> sparklyr</a></li>
<li class="chapter" data-level="8.5.2" data-path="database-big-data.html"><a href="database-big-data.html#RClickhouse"><i class="fa fa-check"></i><b>8.5.2</b> RClickhouse</a></li>
</ul></li>
<li class="chapter" data-level="8.6" data-path="database-FAQ.html"><a href="database-FAQ.html"><i class="fa fa-check"></i><b>8.6</b> 常见问题</a>
<ul>
<li class="chapter" data-level="8.6.1" data-path="database-FAQ.html"><a href="database-FAQ.html#乱码问题"><i class="fa fa-check"></i><b>8.6.1</b> 乱码问题</a></li>
<li class="chapter" data-level="8.6.2" data-path="database-FAQ.html"><a href="database-FAQ.html#无法连接问题"><i class="fa fa-check"></i><b>8.6.2</b> 无法连接问题</a></li>
<li class="chapter" data-level="8.6.3" data-path="database-FAQ.html"><a href="database-FAQ.html#远程连接"><i class="fa fa-check"></i><b>8.6.3</b> 远程连接</a></li>
<li class="chapter" data-level="8.6.4" data-path="database-FAQ.html"><a href="database-FAQ.html#跳板机连接"><i class="fa fa-check"></i><b>8.6.4</b> 跳板机连接</a></li>
</ul></li>
<li class="chapter" data-level="8.7" data-path="database-dbplyr-package.html"><a href="database-dbplyr-package.html"><i class="fa fa-check"></i><b>8.7</b> dbplyr</a>
<ul>
<li class="chapter" data-level="8.7.1" data-path="database-dbplyr-package.html"><a href="database-dbplyr-package.html#安装-3"><i class="fa fa-check"></i><b>8.7.1</b> 安装</a></li>
<li class="chapter" data-level="8.7.2" data-path="database-dbplyr-package.html"><a href="database-dbplyr-package.html#基础用法-1"><i class="fa fa-check"></i><b>8.7.2</b> 基础用法</a></li>
<li class="chapter" data-level="8.7.3" data-path="database-dbplyr-package.html"><a href="database-dbplyr-package.html#无法正确转化"><i class="fa fa-check"></i><b>8.7.3</b> 无法正确转化</a></li>
</ul></li>
<li class="chapter" data-level="8.8" data-path="database-reference-material.html"><a href="database-reference-material.html"><i class="fa fa-check"></i><b>8.8</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="R-loop-structure.html"><a href="R-loop-structure.html"><i class="fa fa-check"></i><b>9</b> 循环结构</a>
<ul>
<li class="chapter" data-level="9.1" data-path="R-loop-description.html"><a href="R-loop-description.html"><i class="fa fa-check"></i><b>9.1</b> R循环介绍</a>
<ul>
<li class="chapter" data-level="9.1.1" data-path="R-loop-description.html"><a href="R-loop-description.html#R:loop-example"><i class="fa fa-check"></i><b>9.1.1</b> 简单示例</a></li>
<li class="chapter" data-level="9.1.2" data-path="R-loop-description.html"><a href="R-loop-description.html#three:loop-structure"><i class="fa fa-check"></i><b>9.1.2</b> 循环结构</a></li>
<li class="chapter" data-level="9.1.3" data-path="R-loop-description.html"><a href="R-loop-description.html#loop:control"><i class="fa fa-check"></i><b>9.1.3</b> 循环控制</a></li>
<li class="chapter" data-level="9.1.4" data-path="R-loop-description.html"><a href="R-loop-description.html#嵌套循环"><i class="fa fa-check"></i><b>9.1.4</b> 嵌套循环</a></li>
</ul></li>
<li class="chapter" data-level="9.2" data-path="loop-changes.html"><a href="loop-changes.html"><i class="fa fa-check"></i><b>9.2</b> 循环变化</a>
<ul>
<li class="chapter" data-level="9.2.1" data-path="loop-changes.html"><a href="loop-changes.html#修改已有对象"><i class="fa fa-check"></i><b>9.2.1</b> 修改已有对象</a></li>
<li class="chapter" data-level="9.2.2" data-path="loop-changes.html"><a href="loop-changes.html#循环模式"><i class="fa fa-check"></i><b>9.2.2</b> 循环模式</a></li>
<li class="chapter" data-level="9.2.3" data-path="loop-changes.html"><a href="loop-changes.html#未知长度输出"><i class="fa fa-check"></i><b>9.2.3</b> 未知长度输出</a></li>
</ul></li>
<li class="chapter" data-level="9.3" data-path="apply-function.html"><a href="apply-function.html"><i class="fa fa-check"></i><b>9.3</b> apply系列函数</a>
<ul>
<li class="chapter" data-level="9.3.1" data-path="apply-function.html"><a href="apply-function.html#lapply"><i class="fa fa-check"></i><b>9.3.1</b> lapply 循环迭代</a></li>
<li class="chapter" data-level="9.3.2" data-path="apply-function.html"><a href="apply-function.html#sapply"><i class="fa fa-check"></i><b>9.3.2</b> sapply</a></li>
<li class="chapter" data-level="9.3.3" data-path="apply-function.html"><a href="apply-function.html#vapply"><i class="fa fa-check"></i><b>9.3.3</b> vapply 迭代安全函数</a></li>
<li class="chapter" data-level="9.3.4" data-path="apply-function.html"><a href="apply-function.html#apply"><i class="fa fa-check"></i><b>9.3.4</b> apply 多维数据</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="10" data-path="purrr-Iteration.html"><a href="purrr-Iteration.html"><i class="fa fa-check"></i><b>10</b> 循环迭代purrr介绍</a>
<ul>
<li class="chapter" data-level="10.1" data-path="purrr-basic-usage.html"><a href="purrr-basic-usage.html"><i class="fa fa-check"></i><b>10.1</b> 简单用法</a></li>
<li class="chapter" data-level="10.2" data-path="purrr-map.html"><a href="purrr-map.html"><i class="fa fa-check"></i><b>10.2</b> map系列常用函数</a></li>
<li class="chapter" data-level="10.3" data-path="purrr-reduce.html"><a href="purrr-reduce.html"><i class="fa fa-check"></i><b>10.3</b> 归约累计函数</a></li>
<li class="chapter" data-level="10.4" data-path="purrr-safely-functions.html"><a href="purrr-safely-functions.html"><i class="fa fa-check"></i><b>10.4</b> 安全函数</a></li>
<li class="chapter" data-level="10.5" data-path="purrr-multi-parameter.html"><a href="purrr-multi-parameter.html"><i class="fa fa-check"></i><b>10.5</b> 映射多个参数</a></li>
<li class="chapter" data-level="10.6" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html"><i class="fa fa-check"></i><b>10.6</b> 其他函数介绍</a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="self-define-function.html"><a href="self-define-function.html"><i class="fa fa-check"></i><b>11</b> 自定义函数</a>
<ul>
<li class="chapter" data-level="11.1" data-path="define-function-example.html"><a href="define-function-example.html"><i class="fa fa-check"></i><b>11.1</b> 简单示例</a></li>
<li class="chapter" data-level="11.2" data-path="define-function-conditional-execution.html"><a href="define-function-conditional-execution.html"><i class="fa fa-check"></i><b>11.2</b> 条件执行</a>
<ul>
<li class="chapter" data-level="11.2.1" data-path="define-function-conditional-execution.html"><a href="define-function-conditional-execution.html#多条件执行"><i class="fa fa-check"></i><b>11.2.1</b> 多条件执行</a></li>
</ul></li>
<li class="chapter" data-level="11.3" data-path="define-function-parameter.html"><a href="define-function-parameter.html"><i class="fa fa-check"></i><b>11.3</b> 函数参数</a>
<ul>
<li class="chapter" data-level="11.3.1" data-path="define-function-parameter.html"><a href="define-function-parameter.html#参数名称"><i class="fa fa-check"></i><b>11.3.1</b> 参数名称</a></li>
<li class="chapter" data-level="11.3.2" data-path="define-function-parameter.html"><a href="define-function-parameter.html#检查参数值"><i class="fa fa-check"></i><b>11.3.2</b> 检查参数值</a></li>
<li class="chapter" data-level="11.3.3" data-path="define-function-parameter.html"><a href="define-function-parameter.html#参数-5"><i class="fa fa-check"></i><b>11.3.3</b> …参数</a></li>
</ul></li>
<li class="chapter" data-level="11.4" data-path="function-return.html"><a href="function-return.html"><i class="fa fa-check"></i><b>11.4</b> 返回值</a>
<ul>
<li class="chapter" data-level="11.4.1" data-path="function-return.html"><a href="function-return.html#显式返回"><i class="fa fa-check"></i><b>11.4.1</b> 显式返回</a></li>
<li class="chapter" data-level="11.4.2" data-path="function-return.html"><a href="function-return.html#编写管道函数"><i class="fa fa-check"></i><b>11.4.2</b> 编写管道函数</a></li>
</ul></li>
<li class="chapter" data-level="11.5" data-path="function-environment.html"><a href="function-environment.html"><i class="fa fa-check"></i><b>11.5</b> 环境</a></li>
<li class="chapter" data-level="11.6" data-path="function-expand.html"><a href="function-expand.html"><i class="fa fa-check"></i><b>11.6</b> 拓展部分</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="file-system-operation.html"><a href="file-system-operation.html"><i class="fa fa-check"></i><b>12</b> R语言文件系统操作</a>
<ul>
<li class="chapter" data-level="12.1" data-path="file-base-r.html"><a href="file-base-r.html"><i class="fa fa-check"></i><b>12.1</b> base R</a>
<ul>
<li class="chapter" data-level="12.1.1" data-path="file-base-r.html"><a href="file-base-r.html#base:r-usage"><i class="fa fa-check"></i><b>12.1.1</b> 用法</a></li>
</ul></li>
<li class="chapter" data-level="12.2" data-path="fs-package.html"><a href="fs-package.html"><i class="fa fa-check"></i><b>12.2</b> fs package</a>
<ul>
<li class="chapter" data-level="12.2.1" data-path="fs-package.html"><a href="fs-package.html#fs-install"><i class="fa fa-check"></i><b>12.2.1</b> 安装</a></li>
<li class="chapter" data-level="12.2.2" data-path="fs-package.html"><a href="fs-package.html#fs-advanced"><i class="fa fa-check"></i><b>12.2.2</b> 优势</a></li>
<li class="chapter" data-level="12.2.3" data-path="fs-package.html"><a href="fs-package.html#fs-usage"><i class="fa fa-check"></i><b>12.2.3</b> 基础用法</a></li>
</ul></li>
<li class="chapter" data-level="12.3" data-path="fs-functions.html"><a href="fs-functions.html"><i class="fa fa-check"></i><b>12.3</b> fs函数介绍</a>
<ul>
<li class="chapter" data-level="12.3.1" data-path="fs-functions.html"><a href="fs-functions.html#fs-file-manipulation"><i class="fa fa-check"></i><b>12.3.1</b> 文件操作</a></li>
<li class="chapter" data-level="12.3.2" data-path="fs-functions.html"><a href="fs-functions.html#fs-dir-manipulation"><i class="fa fa-check"></i><b>12.3.2</b> 目录操作</a></li>
<li class="chapter" data-level="12.3.3" data-path="fs-functions.html"><a href="fs-functions.html#fs-link-manipulation"><i class="fa fa-check"></i><b>12.3.3</b> 链接操作</a></li>
<li class="chapter" data-level="12.3.4" data-path="fs-functions.html"><a href="fs-functions.html#fs-path-manipulation"><i class="fa fa-check"></i><b>12.3.4</b> 路径操作</a></li>
<li class="chapter" data-level="12.3.5" data-path="fs-functions.html"><a href="fs-functions.html#fs-help-manipulation"><i class="fa fa-check"></i><b>12.3.5</b> 辅助函数</a></li>
</ul></li>
<li class="chapter" data-level="12.4" data-path="file-base-fs-difference.html"><a href="file-base-fs-difference.html"><i class="fa fa-check"></i><b>12.4</b> base-fs-shell比较</a></li>
<li class="chapter" data-level="12.5" data-path="file-manipulation-reference-material.html"><a href="file-manipulation-reference-material.html"><i class="fa fa-check"></i><b>12.5</b> 参考资料</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://gitee.com/zhongyufei/Rbook" target="blank">gitee地址</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">商业数据分析师-R 语言数据处理</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="函数功能" class="section level2" number="6.2">
<h2><span class="header-section-number">6.2</span> 函数功能</h2>
<p>主要介绍<code>fct_reorder()</code>，<code>fct_infreq()</code>，<code>fct_relevel()</code>等函数用法。</p>
<div id="创建因子" class="section level3" number="6.2.1">
<h3><span class="header-section-number">6.2.1</span> 创建因子</h3>
<p>使用 base R 中 <code>factor</code>函数创建因子。</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="函数功能.html#cb3-1" aria-hidden="true" tabindex="-1"></a>x1 <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">&quot;c事业部&quot;</span>,<span class="st">&quot;a事业部&quot;</span>,<span class="st">&quot;b事业部&quot;</span>)</span>
<span id="cb3-2"><a href="函数功能.html#cb3-2" aria-hidden="true" tabindex="-1"></a>x2 <span class="ot">&lt;-</span> <span class="fu">factor</span>(x1)</span>
<span id="cb3-3"><a href="函数功能.html#cb3-3" aria-hidden="true" tabindex="-1"></a>x3 <span class="ot">&lt;-</span> <span class="fu">factor</span>(x1,<span class="at">levels =</span> x1)</span>
<span id="cb3-4"><a href="函数功能.html#cb3-4" aria-hidden="true" tabindex="-1"></a>x2</span>
<span id="cb3-5"><a href="函数功能.html#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] c事业部 a事业部 b事业部</span></span>
<span id="cb3-6"><a href="函数功能.html#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: a事业部 b事业部 c事业部</span></span>
<span id="cb3-7"><a href="函数功能.html#cb3-7" aria-hidden="true" tabindex="-1"></a>x3</span>
<span id="cb3-8"><a href="函数功能.html#cb3-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] c事业部 a事业部 b事业部</span></span>
<span id="cb3-9"><a href="函数功能.html#cb3-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: c事业部 a事业部 b事业部</span></span></code></pre></div>
<p>通过观察发现因子<code>x2</code>,<code>x3</code>的顺序不一致。在我们做报表的时候，可以利用这个特性给事业部或者是别的类似字段指定因子水平达到排序效果。</p>
<p>当某水平因子未指定时，会被替换成NA,如下所示：</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="函数功能.html#cb4-1" aria-hidden="true" tabindex="-1"></a>x4 <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">&quot;d事业部&quot;</span>,<span class="st">&quot;a事业部&quot;</span>,<span class="st">&quot;b事业部&quot;</span>)</span>
<span id="cb4-2"><a href="函数功能.html#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="fu">factor</span>(x1,<span class="at">levels =</span> x4)</span>
<span id="cb4-3"><a href="函数功能.html#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &lt;NA&gt;    a事业部 b事业部</span></span>
<span id="cb4-4"><a href="函数功能.html#cb4-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: d事业部 a事业部 b事业部</span></span></code></pre></div>
<p>创建因子想因子顺序和初始顺序一样，可以通过如下方式创建。</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="函数功能.html#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(forcats)</span>
<span id="cb5-2"><a href="函数功能.html#cb5-2" aria-hidden="true" tabindex="-1"></a>x1 <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">&quot;Dec&quot;</span>, <span class="st">&quot;Apr&quot;</span>, <span class="st">&quot;Jan&quot;</span>, <span class="st">&quot;Mar&quot;</span>)</span>
<span id="cb5-3"><a href="函数功能.html#cb5-3" aria-hidden="true" tabindex="-1"></a>f2 <span class="ot">&lt;-</span> x1 <span class="sc">%&gt;%</span> <span class="fu">factor</span>() <span class="sc">%&gt;%</span> <span class="fu">fct_inorder</span>()</span></code></pre></div>
</div>
<div id="修改因子顺序" class="section level3" number="6.2.2">
<h3><span class="header-section-number">6.2.2</span> 修改因子顺序</h3>
<p>在我们作图时更改因子级别的顺序往往很有用，如下所示：</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="函数功能.html#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb6-2"><a href="函数功能.html#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; -- Attaching packages --------------------------------------- tidyverse 1.3.1 --</span></span>
<span id="cb6-3"><a href="函数功能.html#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; v ggplot2 3.3.3     v purrr   0.3.4</span></span>
<span id="cb6-4"><a href="函数功能.html#cb6-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; v tibble  3.1.2     v dplyr   1.0.6</span></span>
<span id="cb6-5"><a href="函数功能.html#cb6-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; v tidyr   1.1.3     v stringr 1.4.0</span></span>
<span id="cb6-6"><a href="函数功能.html#cb6-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; v readr   1.4.0</span></span>
<span id="cb6-7"><a href="函数功能.html#cb6-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; -- Conflicts ------------------------------------------ tidyverse_conflicts() --</span></span>
<span id="cb6-8"><a href="函数功能.html#cb6-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; x dplyr::filter() masks stats::filter()</span></span>
<span id="cb6-9"><a href="函数功能.html#cb6-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; x dplyr::lag()    masks stats::lag()</span></span>
<span id="cb6-10"><a href="函数功能.html#cb6-10" aria-hidden="true" tabindex="-1"></a>relig_summary <span class="ot">&lt;-</span> gss_cat <span class="sc">%&gt;%</span></span>
<span id="cb6-11"><a href="函数功能.html#cb6-11" aria-hidden="true" tabindex="-1"></a>  <span class="fu">group_by</span>(relig) <span class="sc">%&gt;%</span></span>
<span id="cb6-12"><a href="函数功能.html#cb6-12" aria-hidden="true" tabindex="-1"></a>  <span class="fu">summarise</span>(</span>
<span id="cb6-13"><a href="函数功能.html#cb6-13" aria-hidden="true" tabindex="-1"></a>    <span class="at">age =</span> <span class="fu">mean</span>(age, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb6-14"><a href="函数功能.html#cb6-14" aria-hidden="true" tabindex="-1"></a>    <span class="at">tvhours =</span> <span class="fu">mean</span>(tvhours, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb6-15"><a href="函数功能.html#cb6-15" aria-hidden="true" tabindex="-1"></a>    <span class="at">n =</span> <span class="fu">n</span>()</span>
<span id="cb6-16"><a href="函数功能.html#cb6-16" aria-hidden="true" tabindex="-1"></a>  )</span>
<span id="cb6-17"><a href="函数功能.html#cb6-17" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(relig_summary, <span class="fu">aes</span>(tvhours, relig)) <span class="sc">+</span> <span class="fu">geom_point</span>()</span></code></pre></div>
<p><img src="Data-Handling-in-R_files/figure-html/unnamed-chunk-7-1.png" width="90%" style="display: block; margin: auto;" /></p>
<blockquote>
<p>gss_cat 是 forcats 包自带数据集，展示的是宗教间每天看电视的平均小时数</p>
</blockquote>
<p>根据绘制的图像，我们很难有效解释，因为图表是杂乱无序的。我们通过<code>fct_reorder()</code>重新指定因子水平使图表表达更加清晰。</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="函数功能.html#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(relig_summary, <span class="fu">aes</span>(tvhours, <span class="fu">fct_reorder</span>(relig, tvhours))) <span class="sc">+</span></span>
<span id="cb7-2"><a href="函数功能.html#cb7-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_point</span>()</span></code></pre></div>
<p><img src="Data-Handling-in-R_files/figure-html/unnamed-chunk-8-1.png" width="90%" style="display: block; margin: auto;" /></p>
<p><code>fct_reorder()</code>三个参数：</p>
<ul>
<li>.f，你想要调整因子水平的因子或者是字符向量</li>
<li>.x，指定因子的水平</li>
<li>.fun，可选的，当一个x对应多个f时的聚合函数，默认是<code>median</code></li>
</ul>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="函数功能.html#cb8-1" aria-hidden="true" tabindex="-1"></a>df <span class="ot">&lt;-</span> tibble<span class="sc">::</span><span class="fu">tribble</span>(</span>
<span id="cb8-2"><a href="函数功能.html#cb8-2" aria-hidden="true" tabindex="-1"></a>  <span class="sc">~</span>color,     <span class="sc">~</span>a, <span class="sc">~</span>b,</span>
<span id="cb8-3"><a href="函数功能.html#cb8-3" aria-hidden="true" tabindex="-1"></a>  <span class="st">&quot;blue&quot;</span>,      <span class="dv">1</span>,  <span class="dv">2</span>,</span>
<span id="cb8-4"><a href="函数功能.html#cb8-4" aria-hidden="true" tabindex="-1"></a>  <span class="st">&quot;green&quot;</span>,     <span class="dv">6</span>,  <span class="dv">2</span>,</span>
<span id="cb8-5"><a href="函数功能.html#cb8-5" aria-hidden="true" tabindex="-1"></a>  <span class="st">&quot;purple&quot;</span>,    <span class="dv">3</span>,  <span class="dv">3</span>,</span>
<span id="cb8-6"><a href="函数功能.html#cb8-6" aria-hidden="true" tabindex="-1"></a>  <span class="st">&quot;red&quot;</span>,       <span class="dv">2</span>,  <span class="dv">3</span>,</span>
<span id="cb8-7"><a href="函数功能.html#cb8-7" aria-hidden="true" tabindex="-1"></a>  <span class="st">&quot;yellow&quot;</span>,    <span class="dv">5</span>,  <span class="dv">1</span></span>
<span id="cb8-8"><a href="函数功能.html#cb8-8" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb8-9"><a href="函数功能.html#cb8-9" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>color <span class="ot">&lt;-</span> <span class="fu">factor</span>(df<span class="sc">$</span>color)</span>
<span id="cb8-10"><a href="函数功能.html#cb8-10" aria-hidden="true" tabindex="-1"></a><span class="fu">fct_reorder</span>(df<span class="sc">$</span>color, df<span class="sc">$</span>a, min)</span>
<span id="cb8-11"><a href="函数功能.html#cb8-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] blue   green  purple red    yellow</span></span>
<span id="cb8-12"><a href="函数功能.html#cb8-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: blue red purple yellow green</span></span>
<span id="cb8-13"><a href="函数功能.html#cb8-13" aria-hidden="true" tabindex="-1"></a><span class="fu">fct_reorder2</span>(df<span class="sc">$</span>color, df<span class="sc">$</span>a, df<span class="sc">$</span>b)</span>
<span id="cb8-14"><a href="函数功能.html#cb8-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] blue   green  purple red    yellow</span></span>
<span id="cb8-15"><a href="函数功能.html#cb8-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: purple red blue green yellow</span></span></code></pre></div>
<p>如果我们想看报告收入水平的平均年龄变化情况？</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="函数功能.html#cb9-1" aria-hidden="true" tabindex="-1"></a>rincome_summary <span class="ot">&lt;-</span> gss_cat <span class="sc">%&gt;%</span></span>
<span id="cb9-2"><a href="函数功能.html#cb9-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">group_by</span>(rincome) <span class="sc">%&gt;%</span></span>
<span id="cb9-3"><a href="函数功能.html#cb9-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">summarise</span>(</span>
<span id="cb9-4"><a href="函数功能.html#cb9-4" aria-hidden="true" tabindex="-1"></a>    <span class="at">age =</span> <span class="fu">mean</span>(age, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb9-5"><a href="函数功能.html#cb9-5" aria-hidden="true" tabindex="-1"></a>    <span class="at">tvhours =</span> <span class="fu">mean</span>(tvhours, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb9-6"><a href="函数功能.html#cb9-6" aria-hidden="true" tabindex="-1"></a>    <span class="at">n =</span> <span class="fu">n</span>()</span>
<span id="cb9-7"><a href="函数功能.html#cb9-7" aria-hidden="true" tabindex="-1"></a>  )</span>
<span id="cb9-8"><a href="函数功能.html#cb9-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-9"><a href="函数功能.html#cb9-9" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(rincome_summary, <span class="fu">aes</span>(age, <span class="fu">fct_reorder</span>(rincome, age))) <span class="sc">+</span> <span class="fu">geom_point</span>()</span></code></pre></div>
<p><img src="Data-Handling-in-R_files/figure-html/unnamed-chunk-10-1.png" width="90%" style="display: block; margin: auto;" /></p>
<p>如果我们像之前一样更改收入的因子水平，但这并不是一个好的决定，因为收入在一般意义上已经有了顺序。但是，如果我们修改特殊的因子水平还是有意义的，如下：</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="函数功能.html#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(rincome_summary, <span class="fu">aes</span>(age, <span class="fu">fct_relevel</span>(rincome, <span class="st">&quot;Not applicable&quot;</span>))) <span class="sc">+</span></span>
<span id="cb10-2"><a href="函数功能.html#cb10-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_point</span>()</span></code></pre></div>
<p><img src="Data-Handling-in-R_files/figure-html/unnamed-chunk-11-1.png" width="90%" style="display: block; margin: auto;" /></p>
<p><code>fct_relevel()</code>用法：</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="函数功能.html#cb11-1" aria-hidden="true" tabindex="-1"></a>f <span class="ot">&lt;-</span> <span class="fu">factor</span>(<span class="fu">c</span>(<span class="st">&quot;a&quot;</span>, <span class="st">&quot;b&quot;</span>, <span class="st">&quot;c&quot;</span>, <span class="st">&quot;d&quot;</span>), <span class="at">levels =</span> <span class="fu">c</span>(<span class="st">&quot;b&quot;</span>, <span class="st">&quot;c&quot;</span>, <span class="st">&quot;d&quot;</span>, <span class="st">&quot;a&quot;</span>))</span>
<span id="cb11-2"><a href="函数功能.html#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="fu">fct_relevel</span>(f)</span>
<span id="cb11-3"><a href="函数功能.html#cb11-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] a b c d</span></span>
<span id="cb11-4"><a href="函数功能.html#cb11-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: b c d a</span></span>
<span id="cb11-5"><a href="函数功能.html#cb11-5" aria-hidden="true" tabindex="-1"></a><span class="fu">fct_relevel</span>(f, <span class="st">&quot;a&quot;</span>)</span>
<span id="cb11-6"><a href="函数功能.html#cb11-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] a b c d</span></span>
<span id="cb11-7"><a href="函数功能.html#cb11-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: a b c d</span></span>
<span id="cb11-8"><a href="函数功能.html#cb11-8" aria-hidden="true" tabindex="-1"></a><span class="fu">fct_relevel</span>(f, <span class="st">&quot;b&quot;</span>, <span class="st">&quot;a&quot;</span>)</span>
<span id="cb11-9"><a href="函数功能.html#cb11-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] a b c d</span></span>
<span id="cb11-10"><a href="函数功能.html#cb11-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: b a c d</span></span>
<span id="cb11-11"><a href="函数功能.html#cb11-11" aria-hidden="true" tabindex="-1"></a><span class="fu">fct_relevel</span>(f, <span class="st">&quot;a&quot;</span>, <span class="at">after =</span> <span class="dv">2</span>)</span>
<span id="cb11-12"><a href="函数功能.html#cb11-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] a b c d</span></span>
<span id="cb11-13"><a href="函数功能.html#cb11-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: b c a d</span></span></code></pre></div>
<p><code>fct_infreq()</code>用法：</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="函数功能.html#cb12-1" aria-hidden="true" tabindex="-1"></a>gss_cat <span class="sc">%&gt;%</span></span>
<span id="cb12-2"><a href="函数功能.html#cb12-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">marital =</span> marital <span class="sc">%&gt;%</span> <span class="fu">fct_infreq</span>() <span class="sc">%&gt;%</span> <span class="fu">fct_rev</span>()) <span class="sc">%&gt;%</span></span>
<span id="cb12-3"><a href="函数功能.html#cb12-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">ggplot</span>(<span class="fu">aes</span>(marital)) <span class="sc">+</span></span>
<span id="cb12-4"><a href="函数功能.html#cb12-4" aria-hidden="true" tabindex="-1"></a>    <span class="fu">geom_bar</span>()</span></code></pre></div>
<p><img src="Data-Handling-in-R_files/figure-html/unnamed-chunk-13-1.png" width="90%" style="display: block; margin: auto;" /></p>
<p>该系列函数功能是改变因子水平。</p>
<p>-<code>fct_inorder()</code>: 以第一次出现的顺序为顺序
-<code>fct_infreq()</code>:以每个因子出现的次数为顺序,数字大在前
-<code>fct_inseq()</code>:以级别的数字为顺序</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="函数功能.html#cb13-1" aria-hidden="true" tabindex="-1"></a>f <span class="ot">&lt;-</span> <span class="fu">factor</span>(<span class="fu">c</span>(<span class="st">&quot;b&quot;</span>, <span class="st">&quot;b&quot;</span>, <span class="st">&quot;a&quot;</span>, <span class="st">&quot;c&quot;</span>, <span class="st">&quot;c&quot;</span>, <span class="st">&quot;c&quot;</span>))</span>
<span id="cb13-2"><a href="函数功能.html#cb13-2" aria-hidden="true" tabindex="-1"></a>f</span>
<span id="cb13-3"><a href="函数功能.html#cb13-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] b b a c c c</span></span>
<span id="cb13-4"><a href="函数功能.html#cb13-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: a b c</span></span>
<span id="cb13-5"><a href="函数功能.html#cb13-5" aria-hidden="true" tabindex="-1"></a><span class="fu">fct_inorder</span>(f)</span>
<span id="cb13-6"><a href="函数功能.html#cb13-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] b b a c c c</span></span>
<span id="cb13-7"><a href="函数功能.html#cb13-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: b a c</span></span>
<span id="cb13-8"><a href="函数功能.html#cb13-8" aria-hidden="true" tabindex="-1"></a><span class="fu">fct_infreq</span>(f)</span>
<span id="cb13-9"><a href="函数功能.html#cb13-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] b b a c c c</span></span>
<span id="cb13-10"><a href="函数功能.html#cb13-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: c b a</span></span>
<span id="cb13-11"><a href="函数功能.html#cb13-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-12"><a href="函数功能.html#cb13-12" aria-hidden="true" tabindex="-1"></a>f <span class="ot">&lt;-</span> <span class="fu">factor</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>, <span class="at">levels =</span> <span class="fu">c</span>(<span class="st">&quot;3&quot;</span>, <span class="st">&quot;2&quot;</span>, <span class="st">&quot;1&quot;</span>))</span>
<span id="cb13-13"><a href="函数功能.html#cb13-13" aria-hidden="true" tabindex="-1"></a>f</span>
<span id="cb13-14"><a href="函数功能.html#cb13-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 1 2 3</span></span>
<span id="cb13-15"><a href="函数功能.html#cb13-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: 3 2 1</span></span>
<span id="cb13-16"><a href="函数功能.html#cb13-16" aria-hidden="true" tabindex="-1"></a><span class="fu">fct_inseq</span>(f)</span>
<span id="cb13-17"><a href="函数功能.html#cb13-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 1 2 3</span></span>
<span id="cb13-18"><a href="函数功能.html#cb13-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: 1 2 3</span></span></code></pre></div>
</div>
<div id="修改因子级别" class="section level3" number="6.2.3">
<h3><span class="header-section-number">6.2.3</span> 修改因子级别</h3>
<p>前面我们通过函数修改因子顺序，我们还可以更改因子的级别。</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="函数功能.html#cb14-1" aria-hidden="true" tabindex="-1"></a>gss_cat <span class="sc">%&gt;%</span></span>
<span id="cb14-2"><a href="函数功能.html#cb14-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">partyid =</span> <span class="fu">fct_recode</span>(partyid,</span>
<span id="cb14-3"><a href="函数功能.html#cb14-3" aria-hidden="true" tabindex="-1"></a>    <span class="st">&quot;Republican, strong&quot;</span>    <span class="ot">=</span> <span class="st">&quot;Strong republican&quot;</span>,</span>
<span id="cb14-4"><a href="函数功能.html#cb14-4" aria-hidden="true" tabindex="-1"></a>    <span class="st">&quot;Republican, weak&quot;</span>      <span class="ot">=</span> <span class="st">&quot;Not str republican&quot;</span>,</span>
<span id="cb14-5"><a href="函数功能.html#cb14-5" aria-hidden="true" tabindex="-1"></a>    <span class="st">&quot;Independent, near rep&quot;</span> <span class="ot">=</span> <span class="st">&quot;Ind,near rep&quot;</span>,</span>
<span id="cb14-6"><a href="函数功能.html#cb14-6" aria-hidden="true" tabindex="-1"></a>    <span class="st">&quot;Independent, near dem&quot;</span> <span class="ot">=</span> <span class="st">&quot;Ind,near dem&quot;</span>,</span>
<span id="cb14-7"><a href="函数功能.html#cb14-7" aria-hidden="true" tabindex="-1"></a>    <span class="st">&quot;Democrat, weak&quot;</span>        <span class="ot">=</span> <span class="st">&quot;Not str democrat&quot;</span>,</span>
<span id="cb14-8"><a href="函数功能.html#cb14-8" aria-hidden="true" tabindex="-1"></a>    <span class="st">&quot;Democrat, strong&quot;</span>      <span class="ot">=</span> <span class="st">&quot;Strong democrat&quot;</span></span>
<span id="cb14-9"><a href="函数功能.html#cb14-9" aria-hidden="true" tabindex="-1"></a>  )) <span class="sc">%&gt;%</span></span>
<span id="cb14-10"><a href="函数功能.html#cb14-10" aria-hidden="true" tabindex="-1"></a>  <span class="fu">count</span>(partyid)</span>
<span id="cb14-11"><a href="函数功能.html#cb14-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 10 x 2</span></span>
<span id="cb14-12"><a href="函数功能.html#cb14-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   partyid                   n</span></span>
<span id="cb14-13"><a href="函数功能.html#cb14-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;fct&gt;                 &lt;int&gt;</span></span>
<span id="cb14-14"><a href="函数功能.html#cb14-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 No answer               154</span></span>
<span id="cb14-15"><a href="函数功能.html#cb14-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 Don&#39;t know                1</span></span>
<span id="cb14-16"><a href="函数功能.html#cb14-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 Other party             393</span></span>
<span id="cb14-17"><a href="函数功能.html#cb14-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 Republican, strong     2314</span></span>
<span id="cb14-18"><a href="函数功能.html#cb14-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 Republican, weak       3032</span></span>
<span id="cb14-19"><a href="函数功能.html#cb14-19" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 Independent, near rep  1791</span></span>
<span id="cb14-20"><a href="函数功能.html#cb14-20" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 4 more rows</span></span></code></pre></div>
<p><code>fct_recode()</code>用法：</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="函数功能.html#cb15-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">factor</span>(<span class="fu">c</span>(<span class="st">&quot;apple&quot;</span>, <span class="st">&quot;bear&quot;</span>, <span class="st">&quot;banana&quot;</span>, <span class="st">&quot;dear&quot;</span>))</span>
<span id="cb15-2"><a href="函数功能.html#cb15-2" aria-hidden="true" tabindex="-1"></a><span class="fu">fct_recode</span>(x, <span class="at">fruit =</span> <span class="st">&quot;apple&quot;</span>, <span class="at">fruit =</span> <span class="st">&quot;banana&quot;</span>)</span>
<span id="cb15-3"><a href="函数功能.html#cb15-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] fruit bear  fruit dear </span></span>
<span id="cb15-4"><a href="函数功能.html#cb15-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: fruit bear dear</span></span></code></pre></div>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="函数功能.html#cb16-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">factor</span>(<span class="fu">c</span>(<span class="st">&quot;apple&quot;</span>, <span class="st">&quot;bear&quot;</span>, <span class="st">&quot;banana&quot;</span>, <span class="st">&quot;dear&quot;</span>))</span>
<span id="cb16-2"><a href="函数功能.html#cb16-2" aria-hidden="true" tabindex="-1"></a>levels <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="at">fruit =</span> <span class="st">&quot;apple&quot;</span>, <span class="at">fruit =</span> <span class="st">&quot;banana&quot;</span>)</span>
<span id="cb16-3"><a href="函数功能.html#cb16-3" aria-hidden="true" tabindex="-1"></a><span class="fu">fct_recode</span>(x, <span class="sc">!!!</span>levels)</span>
<span id="cb16-4"><a href="函数功能.html#cb16-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] fruit bear  fruit dear </span></span>
<span id="cb16-5"><a href="函数功能.html#cb16-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: fruit bear dear</span></span></code></pre></div>

</div>
</div>
<!-- </div> -->
            </section>

          </div>
        </div>
      </div>
<a href="介绍.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="Data-Manipulation-datatable.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"whatsapp": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["Data-Handling-in-R.pdf", "Data-Handling-in-R.epub"],
"toc": {
"collapse": "subsection"
}
});
});
</script>

</body>

</html>
